define(["jquery","bbcare",
        "scprit/evaluatingPublicPages/vm/nbna/nbnaAssessment.ui.js",
        "scprit/evaluatingPublicPages/vm/nbna/assessQuestion.servers.js"],
    function ($,$bb,$ui,$servers) {
        var event = {};
        /*
        * 整合tab;
        * */
        event.tabs =function(data,scoreObj){
            var tab= [],end;
            if(!$.isEmptyObject(scoreObj)){end = "end";}
            for(var item in data){
                var id = [];
                for(var  i = 0 ;i<data[item].length;i++){
                    id.push(data[item][i].id);
                }
                var quesType = item == "行为能力" ? "behaviorAbility":item == "被动肌张力" ?"passiveMuscle":item == "主动肌张力" ?"activeMuscles":item == "原始反射" ?"originalReflection":"generalResponse";
                tab.push({"quesType":quesType,"item":item,"ids":JSON.stringify(id),"end":end});
            }
            return tab;
        } ;
        /*
        * 原始数据处理：
        * */
        event.dataDeal = function(data,scoreObj){
            for(var item in data){
                for(var i = 0;i<data[item].length;i++){
                    var options = data[item][i].option;
                    for(var j = 0;j < options.length;j++){
                        options[j].id = data[item][i].id;
                    }
                    data[item][i].option = JSON.stringify(options);
                    if( scoreObj[data[item][i].id]*1 != 0 && !scoreObj[data[item][i].id]){
                        data[item][i].score = "2";
                        scoreObj[data[item][i].id] = "2";
                    }else{
                        data[item][i].score = scoreObj[data[item][i].id];
                    }
                }
            }
        };
        event.tabClick = function(data){
            $("#tabs").off("click","li");
            $("#tabs").on("click","li",function(){
                isChecked();
                $(this).addClass("checked").siblings().removeClass("checked");
                var item = $(this).attr("item");
                $ui.nbnaInit({"id":"#nbna_questions","data":data[item]});
                $('#nbna_questions>ul[class*="ques-checked"]').click();
            });
        };
        event.quesClick = function(data){
            $("#nbna_questions").off("click","ul");
            $("#nbna_questions").on("click","ul",function(){
                $(this).addClass("ques-checked").siblings().removeClass("ques-checked");
                var options = JSON.parse($(this).attr("options")),score = $(this).attr("score");
                $ui.quesTabsInit({"id":"#ques-tabs","data":options});
                $('#ques-tabs>li.options[val="'+score+'"]').addClass("checked");
            });
        };
        event.optionsClick = function(data,scoreObj,itemArr){
            $("#ques-tabs").off("click","li.options");
            $("#ques-tabs").on("click","li.options",function(){
                $(this).addClass("checked").siblings().removeClass("checked");
                var id = $(this).attr('quesId'),value = $(this).attr("val");
                var idS = JSON.parse($('#tabs>li[class*="checked"]').attr("ids"));
                var item = $('#tabs>li[class*="checked"]').attr("item");
                var end =  $('#tabs>li[class*="checked"]').attr("end");
                scoreObj[id] = value;
                if(value == 'N/A'){
                    if($.inArray(item,itemArr) == -1){
                        itemArr.push(item);
                    }
                    $('#tabs>li[class*="checked"]').removeAttr("end");
                }
                $('#nbna_questions>ul[id="'+id+'"]').attr("score",value);
                for(var i = 0;i<data[item].length;i++){
                    if(data[item][i].id == id ){
                        data[item][i].score = value;
                    }
                }
                event.countTotalScore(scoreObj);
                if(end != "end"){
                    if(zeroHasNA(scoreObj,idS)){
                        $('#tabs>li[class*="checked"]').attr("end","end");
                        var jumpArr = jumpNext(item,itemArr);
                        var tips;
                        if(jumpArr.length > 0){
                            //tips = "该能区题目已经做完,是否进入下一项？";
                        }else{
                            //tips = "题目已经全部做完，是否提交？";
                        }
                        //$bb.toastConfirmShow(callFn,jumpArr,tips);
                    }
                }
            });
        };
        event.submitBtn = function(scoreObj,getParams){
            $("#btn-submit").click(function(){
                submitFn(scoreObj,getParams);
            });
        };
        var callFn = function(params){
            if(params.length > 0){
                $('#tabs>li[item="'+params[0]+'"]').click();
            }else{
                $("#btn-submit").click();
            }
        };
        /*
        * 提交：
        * */
        var submitFn = function(scoreObj,getParams){
            var score = countScore(scoreObj);
            var scoreParams = $servers.scoreParams(getParams.examinationPaperName,{"map":scoreObj},{"map":score},getParams.userId,getParams.taskId);
            var getScoreFn = function(data,getParams){
                $bb.toastShow('提交成功',1500);
                var totalScore = 0;
                for(var item in data.data){
                    totalScore += data.data[item]*1;
                }
                var level = totalScore*1 > 35 ? "合格":"不合格";
                $("." + getParams.className).modal("hide");
                $('textarea[name="'+getParams.textareaName +'"]').val("总分：" + totalScore + "分，" + level);
            };
            var notDo =notDoFn(scoreObj);
            if(notDo.length >0){
                var tips = "该项评测第"+notDo.join("、")+"题还没有做，确定提交么？";
                var fn = function(){
                    $servers.getScore(scoreParams,getScoreFn,getParams);
                };
                $bb.toastConfirmShow(fn,'',tips);
            }else{
                $servers.getScore(scoreParams,getScoreFn,getParams);
            }
        };
        /*
        * 各能区分数计算：
        * */
        var countScore = function(scoreObj){
            var score = {"behaviorAbility":0,"passiveMuscle":0,"activeMuscles":0,"originalReflection":0,"generalResponse":0};
            for(var id in scoreObj){
                var val = scoreObj[id];
                if(val == "N/A"){val=0;}
                if(id=='1'||id=='2'||id=='3'||id=='4'||id=='5'||id=='6') score.behaviorAbility += val*1.0;
                if(id=='7'||id=='8'||id=='9'||id=='10') 				 score.passiveMuscle+=val*1.0;
                if(id=='11'||id=='12'||id=='13'||id=='14') 				 score.activeMuscles+=val*1.0;
                if(id=='15'||id=='16'||id=='17') 						 score.originalReflection+=val*1.0;
                if(id=='18'||id=='19'||id=='20') 						 score.generalResponse+=val*1.0;
            }
            return score;
        };
        /*
        * 总分计算：
        * */
        event.countTotalScore = function(scoreObj){
            var score = 0,length = 0;
            for(var id in scoreObj){
                var val = scoreObj[id];
                if(val == "N/A"){
                    length++;
                }else{
                    score += val*1;
                }
            }
            $("#completion").html(score+"分 + "+ length +"N/A");
        };
        /*
        * 判断当前能区是否含有  "N/A"：
        * */
        var zeroHasNA = function(scoreObj,ids){
            var hasFlag = true;
            for(var i = 0 ; i<ids.length;i++){
                if(scoreObj[ids[i]] == "N/A"){
                    hasFlag = false;
                    break;
                }
            }
            return hasFlag;
        };
        var notDoFn = function(scoreObj){
            var notDo = [];
            for(var item in scoreObj){
                if(scoreObj[item] == "N/A"){
                    notDo.push(item);
                }
            }
            return notDo;
        };
        /*
         * 跳入下一个能区：
         * */
        var jumpNext = function(item,itemArr){
            if($.inArray(item,itemArr) >=0 ){
                itemArr.splice($.inArray(item,itemArr),1);
            }
            return itemArr
        };
        /*
        * 完成状态：
        * */
        var isChecked = function(){
            var itemArr = ["行为能力","被动肌张力","主动肌张力","原始反射","一般反应"];
            for(var i = 0;i<itemArr.length;i++){
                $('#tabs>li[end="end"]').addClass("done");
            }
        };
        return event;
    });